package org.example.studentmanager.controller;

import org.example.studentmanager.common.utils.R;
import org.example.studentmanager.dto.StudentScoreDTO;
import org.example.studentmanager.entity.*;
import org.example.studentmanager.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    // 修改成绩
    @PostMapping("/score/update")
    public R<String> updateScore(@RequestBody Score score) {
        teacherService.updateScore(score);
        return R.success("成绩修改成功");
    }
    // 录入学生成绩
    @PostMapping("/score/insert")
    public R<String> insertScore(@RequestBody Score score) {
        teacherService.insertScore(score);
        return R.success("成绩录入成功");
    }
    // 根据id查询教师信息
    @GetMapping("/query/{id}")
    public R<Teacher> getTeacherInfoById(@PathVariable("id") Integer id) {
        Teacher teacher = teacherService.getTeacherInfoById(id);
        if (teacher == null) {
            return R.error(0, "未查询到该教师");
        } else {
            return R.success(teacher);
        }
    }

    // 查询全部学生成绩
    @GetMapping("/scores")
    @ResponseBody
    public R<List<Score>> findAllScores() {
        return R.success(teacherService.findAllScores());
    }

    // 查询教师选课
    @GetMapping("/courses/{teacherId}")
    public R<List<Course>> getTeacherCourses(@PathVariable("teacherId") Integer teacherId) {
        List<Course> courses = teacherService.findCourseSelectionsById(teacherId);
        return R.success(courses);
    }
    // 获取课程学生列表
    @GetMapping("/course/{courseId}/students")
    public R<List<StudentScoreDTO>> getCourseStudents(@PathVariable("courseId") Integer courseId) {
        List<StudentScoreDTO> students = teacherService.findStudentsByCourseId(courseId);
        return R.success(students);
    }
    // 教师选课-多选
    @PostMapping("/courses/insert")
    public R<String> addCourses(@RequestBody TeacherCourseInfo teacherCourseInfo) {
        teacherService.addCourses(teacherCourseInfo);
        return R.success("选课成功");
    }
}
